package com.travel.modules.asset.controller;

import com.travel.commons.utils.PageUtils;
import com.travel.commons.utils.R;
import com.travel.modules.asset.entity.Strategy;
import com.travel.modules.asset.service.StrategyService;
import com.travel.modules.sys.service.SysUserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/static/strategy")
public class StrategyController {
    @Resource
    private StrategyService strategyService;

    @Resource
    private SysUserService sysUserService;

    @RequestMapping("/list")
    public R list(@RequestBody Map<String, Object> param) {
        Integer page = (Integer) param.get("page");
        Integer limit = (Integer) param.get("limit");
        if (page == null || limit == null) {
            return R.error("缺少分页参数");
        }

        String keyword = (String) param.get("keyword");
        List<Strategy> list = strategyService.ftQueryStrategies(StringUtils.isBlank(keyword) ? null : keyword.split(" "));
        return R.ok().put("page", PageUtils.getPage(list, limit, page));
    }

    @RequestMapping("/info")
    public R info(@RequestBody Strategy form) {
        Strategy strategy = strategyService.getOneStrategy(form.getStrategyId());
        strategy.setUsername(sysUserService.getById(strategy.getUserId()).getUsername());
        return R.ok().put("page", strategy);
    }
}
